package com.yuanjin.activity;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.yuanjin.R;
import com.yuanjin.util.LoaddataThread;
import com.yuanjin.util.YuanJin_Application;

public class LoginPagesave extends Activity {
	// 请求服务端的url
	private static String PATH = "http://1.zhan414.sinaapp.com/?/index.php?c=main&a=login";
	// http://1.zhan414.sinaapp.com/?/index.php?c=main&a=write
	private static URL url;
	private TextView TVusername;
	private TextView TVpassword;
	private Button mButton;
	private String data = "";
	private int sum = 0;

	private static final int requestCode = 1;

	static {
		try {
			url = new URL(PATH);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch (msg.what) {
			case 1:
				Toast.makeText(LoginPagesave.this, "注册成功", Toast.LENGTH_SHORT)
						.show();
				break;
			case 2:
				Toast.makeText(LoginPagesave.this, "该用户名已被注册", Toast.LENGTH_SHORT)
						.show();
				break;
			case 3:
				Toast.makeText(LoginPagesave.this, "用户名不存在，请重试或注册新账户",
						Toast.LENGTH_LONG).show();
				break;
			case 4:
				Toast.makeText(LoginPagesave.this, "密码不正确，请重新输入",
						Toast.LENGTH_SHORT).show();
				break;
			case 5:
				Toast.makeText(LoginPagesave.this, "登录验证通过", Toast.LENGTH_SHORT)
						.show();
				/*
				 * receiveData:服务器传过来的用户数据 receiveData_Sum：用户数据的数目
				 */
				data = (String) msg.obj;
				sum = msg.arg1;

				YuanJin_Application md = (YuanJin_Application) getApplication();
				md.setReceiveData(data);
				md.setReceiveData_Sum(sum);

				Intent intent2 = new Intent(LoginPagesave.this, FriendsListActivity.class);
				startActivity(intent2);
				break;
			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login2);

		/* 设置mButton的onClick事件处理 */
		mButton = (Button) findViewById(R.id.loginbutton);
		TVpassword = (TextView) findViewById(R.id.login_password);
		TVusername = (TextView) findViewById(R.id.login_username);

	}

	public void gotoRegisterPage(View view) {
		Intent intent = new Intent(this, RegisterPage.class);
		startActivityForResult(intent, requestCode);
	}

	public void LoginClick(View view) {
		// 将用户名存到本地
		YuanJin_Application yd = new YuanJin_Application();
		yd.setUsername(TVusername.getText().toString());
		// 上传服务器的账号、密码等信息
		Map<String, String> map = new HashMap<String, String>();
		map.put("username", TVusername.getText().toString());
		map.put("password", TVpassword.getText().toString());
		// 上传服务器的调用方法地址
		String path = "http://1.zhan414.sinaapp.com/?/index.php?c=main&a=login";
		if (TVusername.getText().toString() == "") {
			Toast.makeText(LoginPagesave.this, "请输入用户名", Toast.LENGTH_SHORT).show();
		} else {
			// 开启上传线程
			LoaddataThread loaddataThread = new LoaddataThread(map, handler,
					path);
			loaddataThread.start();
		}

		// public void onClick(View v) {
		//
		// Thread thread = new Thread(new Runnable() {
		//
		// @Override
		// public void run() {
		// // TODO Auto-generated method stub
		// String path =
		// "http://1.zhan414.sinaapp.com/?/index.php?c=main&a=write";
		// Map<String, String> map = new HashMap<String, String>();
		// map.put("username", TVusername.getText().toString());
		// map.put("password", TVpassword.getText().toString());
		// // Return= sendHttpClientPost(path, map, "utf-8");
		// HttpUpload httpUpload = new HttpUpload();
		// String result = httpUpload.sendHttpClientPost(path, map,
		// "utf-8");
		// String[] resultarray = new String[2];
		// resultarray = result.split("###");
		// // substring(index):截取该位置（包括该位置）后所有字�?
		// // ﻿因为返回的字符前带有一个字�?�?，如:"?哈哈",因�?�?��将其去掉,而且第一个有效字符位于第4位
		// result = resultarray[0].substring(3);//
		// 经多次实验，从3开始截取才真正完全取到服务器发送下来的数据，否则会带有一些空格的字符
		// Log.e("logtest", result + "nihao");
		// Looper.prepare();
		// if (result.equals("注册成功")) {
		// handler.sendEmptyMessage(1);
		// } else if (result.equals("用户名已存在")) {
		// handler.sendEmptyMessage(2);
		// }
		// }
		//
		// });
		//
		// thread.start();
		//
		// // }
	}
}
